package daily_exercise;

import java.io.IOException;

public class reverseList {

    public static class LinkedNode{
        LinkedNode next;
        int val;

        public LinkedNode(int v){
            this.val = v;
        }
    }
    // 递归
    public static LinkedNode reverse(LinkedNode node){
        if (node == null || node.next == null){
            return node;
        }
        LinkedNode p = reverse(node.next);
        node.next.next = node;
        node.next = null;
        return p;
    }

    // 迭代
    public static LinkedNode reverse2(LinkedNode node){
        if (node == null){
            return null;
        }
        LinkedNode pre = null;
        LinkedNode cur = node;
        while (cur != null){
            LinkedNode nextTempNode = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nextTempNode;
        }
        return pre;
    }

    public static void main(String[] args) throws IOException {
        LinkedNode node = new LinkedNode(1);
        node.next = new LinkedNode(2);
        node.next = new LinkedNode(3);
        node.next = new LinkedNode(4);
        node.next = new LinkedNode(5);

        LinkedNode reverseNode = reverse(node);

    }
}
